<?php
$notifications = array();
$items = array();

$tags_lbl = BUtils::T('tags_lbl');
$special_lbl = BUtils::T('special_lbl');
$see_detail_lbl = BUtils::T('see_detail_lbl');

$html = '';
$markers = array();
$total = count($rows);
foreach ($rows as $k => $r) {
	//if (empty($r['latitude']) or empty($r['longitude'])) {
	//	continue;
	//}
	
	if (!empty($r['distance'])) {
		if ($r['distance'] < 1) $r['distance'] = sprintf('%d m', $r['distance'] * 1000);
		else $r['distance'] = sprintf('%.1f km', $r['distance']);
		
		$r['distance'] = '<i class="gicon-direction"></i> ' . $r['distance'];
	}
	
	$features = array();
	if (!empty($r['cuisine_style'])) {
		$array = explode(',', $r['cuisine_style']);
		foreach ($array as $item) {
			$features[] = array(
				'key' => $item,
				'name' => BUtils::T($item),
			);
		}
	}
	if (!empty($r['cuisine_type'])) {
		$array = explode(',', $r['cuisine_type']);
		foreach ($array as $item) {
			$features[] = array(
				'key' => $item,
				'name' => BUtils::T($item),
			);
		}
	}
	
	$bizLink = Yii::app()->createUrl($this->id.'/view', array('id'=>$r['id']));
	$bizLogoLink = BUtils::GetBusinessPictureUrl($r['url_logo']);
	
	$bizTagLinks = $this->getTagLinks($r['cuisine_style'], ' ', 1);
	if (!empty($bizTagLinks)) $bizTagLinks .= ' ' . $this->getTagLinks($r['cuisine_type'], ' ', 1);
	if (!empty($bizTagLinks))  $bizTagLinks = '<i class="gicon-cutlery"></i> ' . $bizTagLinks;
	
	$bizSpecials = null;
	if (!empty($r['specials']) and $r['specials'] > 0) {
		$specialListLink = Yii::app()->createUrl('/special/view', array('id'=>$r['id_special']));
		$tips = ($r['specials'] > 1) ? $r['specials'] . ' ' . $special_lbl : $r['specials'] . ' ' . $special_lbl;
		$bizSpecials = '<span class="specialsTab  tooltip-onright" title="'.$tips.'"><a class="link popup-window" href="'.$specialListLink.'">'.$r['specials'].'</a></span>';
	}
	
	$pinId = 'mid_' . $r['id'];
	$pinIndex = $total - $k - 1;
	$html .= <<<HTML
<div class="venueItem clearfix map-locate relative" id="{$pinId}" data-pin-id="{$pinId}" data-pin-i="{$pinIndex}">
	<div class="bizLogo">
		<a href="{$bizLink}"><img src="{$bizLogoLink}" width="60" alt="{$r['name']}" class="img-polaroid" /></a>
	</div>
	<div class="bizInfo">
		<h4 class="bizTitle"><span class="bizName">{$r['name']}</span></h4>
		<div class="bizOffer">{$bizSpecials}</div>
	</div>
	<div class="clearfix"></div>
	<div class="bizFooter">
		<div class="tags left">{$bizTagLinks}</div>
		<div class="distance right">{$r['distance']}</div>
		<div class="clearfix"></div>
	</div>
</div>
HTML;
	
	//create markers
	$bizSpecialsMarker = null;
	if (!empty($r['specials']) and $r['specials'] > 0) {
		$bizSpecialsMarker = '<span class="specialsTab"><a class="link">'.$r['specials'].'</a></span>';
	}
	$marker = array(
		'id' => $r['id'],
		'lat' => $r['latitude'],
		'lng' => $r['longitude'],
		'specials' => (int)$r['specials'],
	);
	$marker['body'] = <<<HTML
<div class="place-sider clearfix relative">
	{$bizSpecialsMarker}
	<div class="bizInfo">
		<div class="name">{$r['name']}</div>
		<div class="address">
			<i class="gicon-google-maps gicon-white"></i> <span class="street">{$r['address']}</span><br />
			<i class="gicon-blank"></i> <span class="city">{$r['postcode']} {$r['city']}</span>
		</div>
HTML;
	if (!empty($r['phone'])) {
		$marker['body'] .= '<div class="phone"><i class="gicon-phone gicon-white"></i> '.$r['phone'].'</div>';
	}
	$marker['body'] .= '<div class="see-detail"><a href="'.$bizLink.'">'.$see_detail_lbl.'</a></div>';
	$marker['body'] .= '</div></div>';
	$markers[] = $marker;
	
	$items[] = array(
		'venue' => array(
			'seq' => $offset + $k + 1,
			'id' => $r['id'],
			'name' => $r['name'],
			'logo' => $r['url_logo'],
			'phone' => $r['phone'],
			'location' => array(
				'address' => $r['address'],
				'lat' => $r['latitude'],
				'lon' => $r['longitude'],
				'distance' => $r['distance'],
				'postalCode' => $r['postcode'],
				'city' => $r['city'],
				'cc' => $r['country_code'],
			),
			'tags' => $features,
			'specials' => (int)$r['specials'],
		),
	);
}

echo json_encode(array(
	'notifications' => $notifications,
	'number' => count($items),
	'items' => $items,
	'html' => $html,
	'markers' => array_reverse($markers),//let the merchant with specials in the last
));